Vue.component('add-node', {//添加分类
    template: `<el-dialog :title="form.Level==1?'添加分类':'添加子分类'" :visible.sync="visibleDialog" width="700px">
    <el-form ref="contract" :model="form" :rules="rules" :inline-message="true">
        <el-descriptions :column="1" border size="small">
            <el-descriptions-item label="分类名称">
                <el-form-item prop="Classify_Name">
                    <el-input v-model.trim="form.Classify_Name" placeholder="请输入分类名称"></el-input>
                </el-form-item>
            </el-descriptions-item>
            <el-descriptions-item label="图片上传">
                <el-form-item prop="Picture_URL">
                    <yl-upload :base="BASE_IMG" v-model.trim="form.Picture_URL"></yl-upload>
                </el-form-item>
            </el-descriptions-item>
        </el-descriptions>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:visible',false)">取消</el-button>
        <el-button :loading="loading" type="primary" @click="submitForm('contract')">提交</el-button>
    </span></el-dialog>`,
    props: ['data', 'visible'],
    data() {
        return {
            visibleDialog: false,
            form: {
                Classify_SerialNumber: '',
                ...this.data,
                Classify_Name: '',
                Picture_URL:'',
            },
            rules: {
                Classify_Name: [{ required: true, message: '请输入菜单名称', trigger: 'blur' }],
            },
            loading: false,
        }
    },
    watch: {
        visible() {
            this.visibleDialog = this.visible
        },
        visibleDialog(val) {
            this.$emit('update:visible', val)
        },
        'data.Parent_SerialNumber': function (newVal) {
            this.form.Parent_SerialNumber = newVal;
        },
        'data.Level': function (newVal) {
            this.form.Level = newVal;
        }
    },
    methods: {
        submitForm(formName) {
            const that = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    that.loading = true;
                    $.ajax({
                        headers:requestHeaders(),
                        url: `${BASE_URL2}/generalBusiness/generalSave`,
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({ Add: { 
                                business_classify: [that.form],
                                base_operation_log:[generateLog('添加'+that.form.Level+'级商品分类', '平台端操作', 'business_classify', JSON.stringify({  Add: {  business_classify: [that.form] } }))] 
                            } })
                        },
                        success: function (res) {
                            if (res.Status === 'success') {
                                that.form = { ...that.data, Classify_Name: '', }
                                that.$message.success("提交成功")
                                that.$emit('update:visible', false)
                                that.$emit('success')
                            } else {
                                that.$message.error("提交失败，请稍后再试")
                            }
                        },
                        complete: function (XMLHttpRequest) {
                            that.loading = false
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json"
                    })
                } else {
                    that.$message({
                        message: sumberError,
                        type: 'warning'
                    })
                    return false;
                }
            })
        }
    },
})
Vue.component('edit-node', {//修改分类
    template: `<el-dialog title="修改分类" :visible.sync="visibleDialog" width="700px">
    <el-form ref="contract" :model="form" :rules="rules" :inline-message="true">
        <el-descriptions :column="1" border size="small">
            <el-descriptions-item label="分类名称">
                <el-form-item prop="Classify_Name">
                    <el-input v-model.trim="form.Classify_Name" placeholder="请输入分类名称"></el-input>
                </el-form-item>
            </el-descriptions-item>
            <el-descriptions-item label="图片上传">
                <el-form-item prop="Picture_URL">
                    <yl-upload :base="BASE_IMG" v-model.trim="form.Picture_URL"></yl-upload>
                </el-form-item>
            </el-descriptions-item>
        </el-descriptions>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:visible',false)">取消</el-button>
        <el-button :loading="loading" type="primary" @click="submitForm('contract')">提交</el-button>
    </span></el-dialog>`,
    props: ['id','visible'],
    data() {
        return {
            visibleDialog: false,
            form: {
                Classify_SerialNumber:'',
                Classify_Name:'',
                Picture_URL:'',
            },
            rules: {
                Classify_Name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
            },
            loading: false,
        }
    },
    watch: {
        visible() {
            this.visibleDialog = this.visible
        },
        visibleDialog(val) {
            this.$emit('update:visible', val)
            if (val) {
                this.loading = true,
                this.getData(this.id);
            }
        },
    },
    methods: {
        getData(Classify_SerialNumber){
            const that = this;
            $.ajax({
                headers:requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableNameWithOriginal`,
                type: 'POST',
                data: { 
                    Data: "business_classify" ,
                    Condition: "Classify_SerialNumber='" + Classify_SerialNumber + "'"
                },
                success: function (data) {
                    if (data.length == 1) {
                        that.form = data[0];
                    }
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false;
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        submitForm(formName) {
            const that = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    that.loading = true;
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${BASE_URL2}/generalBusiness/generalSave`,
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({ 
                                Modify: { business_classify: [that.form] } ,
                                Add: {base_operation_log:[generateLog('修改'+that.form.Level+'级商品分类', '平台端操作', 'business_classify', JSON.stringify({  Modify: { business_classify: [that.form] } }))] }
                            })
                        },
                        success: function (res) {
                            if (res.Status === 'success') {
                                that.form =  {
                                    Classify_SerialNumber:'',
                                    Classify_Name:'',
                                }
                                that.$message.success("提交成功")
                                that.$emit('update:visible', false)
                                that.$emit('success')
                            } else {
                                that.$message.error("提交失败，请稍后再试")
                            }
                        },
                        error: function(){
                            that.$message.error("提交失败，该手机号已存在")
                        },
                        complete: function (XMLHttpRequest) {
                            that.loading = false
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json"
                    })
                } else {
                    that.$message({
                        message: sumberError,
                        type: 'warning'
                    })
                    return false;
                }
            })
        }
    },
})
new Vue({
    el: '#app',
    data() {
        return {
            defaultProps: {
                children: 'Children',
                label: 'Classify_Name'
            },
            tableData: [],
            form: {//查询筛选

            },
            nodeData: {//添加
                Parent_SerialNumber: '',
                Level: 1,
            },
            loading: false,
            dialogVisible: false,
            dialogEditVisible: false,
            Classify_SerialNumber :'',
        }
    },
    mounted() {
        this.getList();
    },
    methods: {
        onSerach() {

        },
        getList() {
            const that = this;
            $.ajax({
                headers:requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/getAllClassify`,
                type: 'GET',
                success: function (data) {
                    that.tableData = data
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        Add(node, data) {
            if (node && data) {
                this.nodeData.Parent_SerialNumber = data.Classify_SerialNumber;
                this.nodeData.Level = node.level + 1;
            } else {
                this.nodeData.Parent_SerialNumber = '';
                this.nodeData.Level = 1;
            }
            this.dialogVisible = true;
        },
        Edit(Classify_SerialNumber) {
            this.Classify_SerialNumber = Classify_SerialNumber
            this.dialogEditVisible = true;
        },
        Remove(row) {
            const that = this
            this.$confirm('警告！此操作将删除该菜单节点, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                let obj = configOriginal(row);
                obj.Row_Status = '1';
                $.ajax({
                    headers:requestHeaders(),
                    url: `${BASE_URL2}/generalBusiness/generalSave`,
                    type: 'POST',
                    data: {
                        Data: JSON.stringify({ 
                            Modify: { business_classify: [obj] } ,
                            Add : { base_operation_log:[generateLog('删除商品分类', '平台端操作', 'business_classify', JSON.stringify({  Modify: { business_classify: [obj] } }))] } 
                        })
                    },
                    success: function (res) {
                        if (res.Status == 'success') {
                            that.getList();
                            that.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }
                    },
                    complete: function (XMLHttpRequest) {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json"
                })
            }).catch(() => {
                this.$message({
                    type: 'warning',
                    message: '已取消操作'
                });
            });
        }
    }
})